﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GameSchool.Models;
using GameSchool.Controllers;

namespace GameSchool.Controllers
{
    public class CourseController : Controller
    {
		GameSchoolRepository repository = new GameSchoolRepository();
        //
        // GET: /Course/
        #region Views


        [Authorize(Roles = "Students")]
        public ActionResult StudentViewCourse()
        {
            return View();
        }

        [Authorize(Roles = "Teachers")]
        public ActionResult TeacherViewCourse()
        {
            return View();
        }

        //Displays form with the fields filled in with data from the database
        public ActionResult CourseEdit(int CID)
        {
			EditCourseViewModel vm = new EditCourseViewModel();
			var teacherLogin = repository.GetTeacherLoginRelationship();
			var c = repository.GetCourseByID(CID);
			vm.courseID = c.courseID;
			vm.courseName = c.title;
            vm.teacherName = teacherLogin.name; 
			vm.courseXP = c.courseXP;
			vm.sidebarCourses = repository.GetCoursesByTeacher(teacherLogin).ToList();
			return View(vm);
        }

        /// <summary>
        /// Creates as many levels as are entered into a course as well as an exam,
        /// Assignment and Lecture for that course that are empty but automatically assigned XP.
        /// </summary>
        /// <param name="formData"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult CourseEdit(FormCollection formData) // Ólöf og Óli
        {
			EditCourseViewModel vm = new EditCourseViewModel();
			AchievementController controller = new AchievementController();
            var courseID = Convert.ToInt32(formData["courseID"]);
            var c = repository.GetCourseByID(courseID);
            vm.numberOfLevels = Convert.ToInt32(formData["numberOfLevels"]);
            var totalXP = c.courseXP;

            var perLevelXP = totalXP / vm.numberOfLevels;

            for (int i = 0; i < vm.numberOfLevels; i++)
			{
                /*Create Levels*/
                Level X = new Level();

                X.courseID = courseID;
                X.intLevelXP = perLevelXP;
                X.title = "Level";
                X.number = i + 1;
                controller.CreateAchievementForLevel(X); //To create achievements for these levels

                repository.AddLevel(X);

                /*Create Assignments*/
                Assignment A = new Assignment
                {
                    levelID = X.levelID,
                    assignmentXP = perLevelXP * 0.7 //Assignment is worth 70% of level
                };
                repository.addAssignment(A);

                /*Create Exams*/
                Exam E = new Exam
                {
                    levelID = X.levelID,
                    examXP = perLevelXP * 0.2 //Exam is worth 20% of level
                };
                repository.AddExam(E);

                /*Create Lectures*/
                Lecture L = new Lecture
                {
                    levelID = X.levelID,
                    lectureXP = perLevelXP * 0.1 //Lecture is worth 10% of level
                };
                repository.AddLecture(L);

                repository.Save();
			}

            return RedirectToAction("../Level/LevelEdit", new { CID = courseID });
           
            
        }


        public ActionResult CreateCourse()
        {
            SupervisorPartialViewModel vm = new SupervisorPartialViewModel();
            vm.courses = repository.GetCourses().ToList();
            vm.teachers = repository.GetTeachers().ToList();
            vm.courseOverview = "lalala";
            return View();
        }

        [HttpPost]
        public ActionResult CreateCourse(FormCollection formData)
        {
			Course c = new Course();
            AchievementController controller = new AchievementController();

			c.title = formData["title"];
			c.courseXP = Convert.ToInt32(formData["courseXP"]);
			c.description = formData["description"];
			repository.AddCourse(c);
			repository.Save();
            controller.CreateAchievementForCourse(c); //Adds an achievement that corresponds with completing the course

			return Redirect("~/Supervisor/SupervisorHome"); 
        }


		public ActionResult AssigningTeacher() // Ólöf and Óli
		{
			AssignTeacherViewModel vm = new AssignTeacherViewModel();
			vm.courseName = repository.GetCourses();
			vm.teacherName = repository.GetTeachers();
			return View(vm);
		}

		[HttpPost]
		public ActionResult AssigningTeacher(FormCollection formData) // Ólöf and Óli
		{
			TeacherCourse tc = new TeacherCourse();
			tc.courseID = Convert.ToInt32(formData["courseName"]);
			tc.teacherID = Convert.ToInt32(formData["teacherName"]);

			repository.AddTeacherCourse(tc.courseID, tc.teacherID);
			repository.Save();

			return Redirect("~/Supervisor/SupervisorHome");
		}

		public ActionResult AssigningStudent()
		{
			AssignStudentViewModel vm = new AssignStudentViewModel();
			vm.courseName = repository.GetCourses();
			vm.studentName = repository.GetStudents();

			return View(vm);
		}

		[HttpPost]
		public ActionResult AssigningStudent(FormCollection formData)
		{
			StudentCourse sc = new StudentCourse();
			sc.courseID = Convert.ToInt32(formData["courseName"]);
			sc.studentID = Convert.ToInt32(formData["studentName"]);

			repository.AddStudentCourse(sc.courseID, sc.studentID);
			repository.Save();

			return Redirect("~/Supervisor/SupervisorHome");
		}

        public ActionResult CourseDescriptionOverview()
        {
            DescriptionViewModel vm = new DescriptionViewModel();
            var studentLogin = repository.GetStudentLoginRelationship();


            vm.Courses = repository.GetCoursesByStudent(studentLogin).ToList();
            
            vm.studentName = studentLogin.name; //For the top bar
            

            vm.sidebarCourses = repository.GetCoursesByStudent(studentLogin).ToList();

            return View(vm);

        }

        public ActionResult CourseDescription(int CID)
        {
            DescriptionViewModel vm = new DescriptionViewModel();
            var studentLogin = repository.GetStudentLoginRelationship();
            var theCourse = repository.GetCourseByID(CID);

            vm.courseDescription = theCourse.description;
            vm.courseName = theCourse.title;
            vm.studentName = studentLogin.name; //For the top bar
            vm.courseID = theCourse.courseID;

            vm.sidebarCourses = repository.GetCoursesByStudent(studentLogin).ToList();

            return View(vm);
        }


        #endregion

        public Notification GetNotifications(Notification theNotification)
        {
            return theNotification;
        }

    }
}
